﻿using UnityEngine;
using System.Collections;
using UniLua;

namespace TCG.FFI
{
	public class UnityTransformLib 
	{
		#region stuff for exporting to lua
		public static string LIB_NAME = "UnityTransformLib";
		#endregion

		#region static variables
		
		#endregion
		
		#region variables
		public static int mf_GetChildCount (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, -1, LIB_NAME) as Transform;
			luaState.PushInteger (trans.childCount);
			return 1;
		}

		public static int mf_GetEulerAngles (ILuaState luaState)
		{
			Transform trans =  LuaTools.L_CheckUData (luaState, -1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.eulerAngles, UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetEulerAngles (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 angles = (Vector3)(LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME));
			trans.eulerAngles = angles;
			return 0;
		}

		public static int mf_GetForward (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, -1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.forward, UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetForward (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 forward = (Vector3)(LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME));
			trans.forward = forward;
			return 0;
		}

		public static int mf_GetHasChanged (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, -1, LIB_NAME) as Transform;
			luaState.PushBoolean (trans.hasChanged);
			return 1;
		}

		public static int mf_SetHasChanged (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			bool flag = luaState.ToBoolean (2);
			trans.hasChanged = flag;
			return 0;
		}

		public static int mf_GetLocalEulerAngles (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.localEulerAngles, UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetLocalEulerAngles (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 angles = (Vector3)(LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME));
			trans.localEulerAngles = angles;
			return 0;
		}

		public static int mf_GetLocalPosition (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.localPosition, UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetLocalPosition (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 localPos = (Vector3)(LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME));
			trans.localPosition = localPos;
			return 0;
		}

		public static int mf_GetLocalRoration (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.localRotation, UnityQuaternionLib.LIB_NAME);
			return 1;
		}

		public static int mf_SetLocalRotation (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Quaternion localR = (Quaternion)LuaTools.L_CheckUData (luaState, 2, UnityQuaternionLib.LIB_NAME);
			trans.localRotation = localR;
			return 0;
		}

		public static int mf_GetLocalScale (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.localScale, UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetLocalScale (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 localScale = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			trans.localScale = localScale;
			return 0;
		}

		public static int mf_GetParent (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			if (trans.parent != null)
				LuaTools.L_PushUserData (luaState, trans.parent, UnityTransformLib.LIB_NAME);
			else
				luaState.PushNil ();
			return 1;
		}

		public static int mf_SetParent (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			if (luaState.IsNil (2))
			{
				trans.parent = null;
			}
			else
			{
				Transform parent = LuaTools.L_CheckUData (luaState, 2, LIB_NAME) as Transform;
				trans.parent = parent;
			}
			return 0;
		}

		public static int mf_GetPosition (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.position, UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetPosition (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 pos = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			trans.position = pos;
			return 0;
		}

		public static int mf_GetRight (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.right, UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetRight (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 right = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			trans.right = right;
			return 0;
		}

		public static int mf_GetRoot (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.root, LIB_NAME);
			return 1;
		}

		public static int mf_GetRotation (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.rotation, UnityQuaternionLib.LIB_NAME);
			return 1;
		}

		public static int mf_SetRotation (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Quaternion rotation = (Quaternion)LuaTools.L_CheckUData (luaState, 2, UnityQuaternionLib.LIB_NAME);
			trans.rotation = rotation;
			return 0;
		}

		public static int mf_GetUp (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			LuaTools.L_PushUserData (luaState, trans.up, UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetUp (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 up = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			trans.up = up;
			return 0;
		}
		
		#endregion
		
		#region constructors
		
		#endregion
		
		#region functions
		public static int mf_DetachChildren (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			trans.DetachChildren ();
			return 0;
		}

		public static int mf_Find (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			string name = luaState.ToString (2);
			Transform result = trans.Find (name);
			if (result == null)
			{
				luaState.PushNil ();
			}
			else
			{
				LuaTools.L_PushUserData (luaState, result, LIB_NAME);
			}
			return 1;
		}

		public static int mf_GetChild (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			int index = luaState.ToInteger (2);
			Transform result = trans.GetChild (index);
			if (result == null)
			{
				luaState.PushNil ();
			}
			else
			{
				LuaTools.L_PushUserData (luaState, result, LIB_NAME);
			}
			return 1;
		}

		public static int mf_InverseTransformDirection (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 direction = (Vector3) LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			LuaTools.L_PushUserData (luaState, trans.InverseTransformDirection (direction), UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_InverseTransformPoint (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 point = (Vector3) LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			LuaTools.L_PushUserData (luaState, trans.InverseTransformPoint (point), UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_is_child_of (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Transform parent = LuaTools.L_CheckUData (luaState, 2, LIB_NAME) as Transform;
			luaState.PushBoolean (trans.IsChildOf (parent));
			return 1;
		}

		public static int mf_LookAt (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 worldPos = (Vector3)LuaTools.L_CheckUData (luaState, 2, LIB_NAME);
			if (luaState.GetTop () == 2)
			{
				trans.LookAt (worldPos);
			}
			else
			{
				Vector3 worldUp = (Vector3)LuaTools.L_CheckUData (luaState, 3, LIB_NAME);
				trans.LookAt (worldPos, worldUp);
			}

			return 0;
		}

		public static int mf_Rotate (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 eulerAngles = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			if (luaState.GetTop () == 2)
			{
				trans.Rotate (eulerAngles);
			}
			else
			{
				int relativeSpace = luaState.ToInteger (3);
				trans.Rotate (eulerAngles, (Space)relativeSpace);
			}
			return 0;
		}

		public static int mf_RotateAxis (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 axis = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			float angle = (float)luaState.ToNumber (3);
			if (luaState.GetTop () == 3)
			{
				trans.Rotate (axis, angle);
			}
			else
			{
				int relativeSpace = luaState.ToInteger (4);
				trans.Rotate (axis, angle, (Space)relativeSpace);
			}

			return 0;
		}

		public static int mf_RotateAround (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 point = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			Vector3 axis = (Vector3)LuaTools.L_CheckUData (luaState, 3, UnityVector3Lib.LIB_NAME);
			float angle = (float)luaState.ToNumber (4);
			trans.RotateAround (point, axis, angle);
			return 0;
		}

		public static int mf_TransformDirection (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 direction = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			trans.TransformDirection (direction);
			return 0;
		}

		public static int mf_TransformPoint (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 point = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			trans.TransformDirection (point);
			return 0;
		}

		public static int mf_Translate (ILuaState luaState)
		{
			Transform trans = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Transform;
			Vector3 direction = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			if (luaState.GetTop () == 2)
			{
				trans.Translate (direction);
			}
			else
			{
				int relativeSpace = luaState.ToInteger (3);
				trans.Translate (direction, (Space)relativeSpace);
			}
			return 0;
		}

		#endregion

		#region static functions

		#endregion

		#region operators

		#endregion
	}
}